package org.dfl.plugin.editor.contributionitems;

import org.dfl.plugin.editor.actions.PauseAction;
import org.dfl.plugin.editor.actions.ResumeAction;
import org.dfl.plugin.editor.model.DflDiagram;
import org.eclipse.jface.action.IAction;
import org.eclipse.swt.widgets.ToolItem;

/**
 * contribution item for pause action
 * @author Piotr Wlodarczyk
 *
 */
public class PauseActionContributionItem extends FlowRunnerActionContributionItem{
	private IAction pauseAction;
	private IAction resumeAction;
	
	@Override
	public void setDiagram(DflDiagram diagram) {
		pauseAction = new PauseAction(null,diagram);
		resumeAction = new ResumeAction(null,diagram);
		super.setDiagram(diagram);
	}

	@Override
	protected void itemSelected() {
		if(pauseAction.isEnabled()){
			pauseAction.run();
		}else{
			resumeAction.run();
		}
	}

	@Override
	protected void updateItem(ToolItem item) {
		if(item!=null){
			if(diagram==null){
				setPauseAction(item,false);
			}else if(pauseAction.isEnabled()){
				setPauseAction(item,true);
			}else if(resumeAction.isEnabled()){
				setResumeAction(item,true);
			}else{
				setPauseAction(item,false);
			}
		}	
	}
	
	private void setPauseAction(ToolItem item,boolean enabled){
		item.setImage(pauseAction.getImageDescriptor().createImage());
		item.setToolTipText(pauseAction.getToolTipText());
		item.setEnabled(enabled);
	}
	
	private void setResumeAction(ToolItem item,boolean enabled){
		item.setImage(resumeAction.getImageDescriptor().createImage());
		item.setToolTipText(resumeAction.getToolTipText());
		item.setEnabled(enabled);
	}
}
